package moa.structure;

import java.util.*;


/**
 * <p>
 * This class implements the ItemSet concept, it is a set of non-empty
 * items (I1 I2 I3 ... Ik) where Ij , j from 1 to k, is an item. This is
 * actually the representation of a transaction but without a date.
 * </p>
 */
public class ItemSet implements Cloneable
{

	/**
	   * <p>
	   *   This is the list containing the items of the itemset
	   * </p>
	   */
	  public List<Integer> itemList = null;


//****************************************************************************************************************************
//Constructor of the class VerticalDB
//****************************************************************************************************************************

	  /**<p>
	   * Create a new empty ItemSet
	   * </p>
	   */
	  public ItemSet() 
	  {
		  itemList = new ArrayList<Integer>(); 
	  }

	  /**<p>
	   * Create a new ItemSet and insert the given item in it
	   * </p>
	   */
	  public ItemSet(int it) 
	  {
		  itemList = new ArrayList<Integer>(); 
		  itemList.add(0, new Integer(it));
	  }

	  /**<p>
	   * Create a new ItemSet and initialize it with a list of items
	   *</p>
	   */
	  public ItemSet(List _iList) 
	  {
		  itemList = _iList;
	  }
	  
	  
//****************************************************************************************************************************
//Accessors of the class VerticalDB
//****************************************************************************************************************************

	  /**<p>
	   * @return the indexth item of the itemset
	   * </p>
	   */
	  public int getItemAt(int index)
	  {
		  if (index < itemList.size())
		  {
			  return (itemList.get(index).intValue());
		  }
		  return -1;
	  }
	  
	  /**
	   * <p>
	   * Accessor to the itemList
	   * </p>
	   *
	   */
	  public List getItemList() 
	  {
		  return itemList;
	  } 

	  /**
	   * <p>
	   * Accessor to modify the itemList
	   * </p>
	   *
	   */
	  public void setItemList(List _ItemList) 
	  {
		  itemList = _ItemList;
	  }
	  
	  
//****************************************************************************************************************************
//Methods of the class VerticalDB
//****************************************************************************************************************************

	  /**
	   * <p>
	   * Adds an item in our itemList (at the end)
	   * </p>
	   * <p>
	   * @param it is the item (int value) to be added
	   * </p>
	   */
	  public void addItem(int it) 
	  {
		  itemList.add(new Integer(it));
	  } 
	 
	  /**
	   * <p>
	   * Adds an item at the beginning of our itemList
	   * </p>
	   * <p>
	   * @param it is the item (int value) to be added
	   * </p>
	   */
	  public void addItemFront(int it)
	  {
		  itemList.add(0, new Integer(it));
	  } 
	  
	  /**<p>
	   * @return true if the itemset already contains the item given in parameter
	   * </p>
	   */
	  public boolean contains(int item)
	  {
		  return(itemList.contains(new Integer(item)));
	  }
	  
	  /**
	   * <p>
	   * Check the size of the itemSet
	   * </p>
	   * <p>
	   * @return a int with the size of itemList
	   * </p>
	   */
	  public int getSize()
	  {
		  return itemList.size();
	  } 
	  
	  /**
	   * <p>
	   * Does a simple reset (empty the list), all the present Items are lost
	   * </p>
	   *
	   */
	  public void reset() 
	  {
		  itemList.clear();
	  }
	  
	  /**
	   * <p>
	   * contains function overloading
	   * </p>
	   * <p>
	   * @return a int with the size of itemList
	   * </p>
	   */
	  public boolean contains(ItemSet item)
	  {
		  for (int i = 0; i < item.getSize();i++)
			  if (!this.contains(item.getItemAt(i)))
				  return false;
		  return true;
		  //return(itemList.contains(item));
	  }

	  
//****************************************************************************************************************************
//Other methods
//****************************************************************************************************************************

	  
	   /**<p> 
	    * This method is called in order to duplicate an itemset 
	    * @return the cloned itemset
	    * </p>
	    */
	  public Object clone()
	  {
		  Object itemset = null;
		  try 
		   {
			  //In order to clone an itemset we have to call the super method clone and then clone the arrayList
			  //containing each item
			  itemset = super.clone();
			  ((ItemSet)(itemset)).itemList =  (ArrayList)((ArrayList)itemList).clone();			 
		   }
		   catch (CloneNotSupportedException e) 
		   {
			   throw new InternalError(e.toString());
		   }
		   return itemset;
		   
	  }
	  
		  
	  public String toString() 
	  {
		  String str = "(";
		  Collections.sort(itemList);
		  for (int i=0; i<getSize(); i++)
		  {
			  if (i != getSize() - 1)
				  str +=  getItemAt(i) +" " ;
			  else
				  str += getItemAt(i);
		  }
		  str +=")";
		  return str;
	  }

} 
